package com.tanhua.model.vo;

import com.tanhua.model.db.Option;
import com.tanhua.model.db.Problem;
import com.tanhua.model.db.Questionnaire;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Mr.Xu
 * @Description:
 * @Date: Created in 19:02 2021/12/25
 * @Modified By:
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class QuestionnaireVo implements Serializable {  //返回问卷vo

    private String id;     //问卷id
    private String name; //问卷名称
    private String cover; //封面图片地址
    private String level; //等级
    private Integer star; //星级
    private List<Object> questions; //试题
    private Integer isLock = 0; // 是否锁住 0解锁 1锁住 默认锁住
    private String reportId; //最新报告id

    public static QuestionnaireVo init(Questionnaire questionnaire){
        QuestionnaireVo questionnaireVo = new QuestionnaireVo();
        BeanUtils.copyProperties(questionnaire, questionnaireVo);
        questionnaireVo.setId(questionnaire.getId().toString());
        List<Object> list = new ArrayList<>();
        for (Problem question : questionnaire.getQuestions()) {
            Map<String,Object> map = new HashMap<>();
            map.put("id", question.getId().toString());
            map.put("question", question.getQuestion());
            List<Object> optionList = new ArrayList<>();
            for (Option option : question.getOptions()) {
                Map<String, String> optionMap = new HashMap<>();
                optionMap.put("id", option.getId().toString());
                optionMap.put("option",option.getOptionDes());
                optionList.add(optionMap);
            }
            map.put("options", optionList);
            list.add(map);
        }
        questionnaireVo.setQuestions(list);
        return questionnaireVo;
    }

}
